import br.com.rise.leilao.Categoria
import br.com.rise.leilao.access.Requestmap
import br.com.rise.leilao.access.Role
import br.com.rise.leilao.access.Usuario
import br.com.rise.leilao.access.UsuarioRole


class BootStrap {

	def springSecurityService

	def init = { servletContext ->
		String password = springSecurityService.encodePassword('123')

		def userAdmin = Usuario.findByUsername("super") ?: new Usuario(nome: "Super User", username: 'super', enabled: true, password: password)
		userAdmin.save(flush: true)
		
		def adminRole = new Role(authority: 'ROLE_ADMIN').save()
		
		UsuarioRole.create userAdmin, adminRole, true

		new Requestmap(url: '/js/**', configAttribute: 	'IS_AUTHENTICATED_ANONYMOUSLY').save()
		new Requestmap(url: '/css/**', configAttribute: 	'IS_AUTHENTICATED_ANONYMOUSLY').save()
		new Requestmap(url: '/images/**', configAttribute: 	'IS_AUTHENTICATED_ANONYMOUSLY').save()
		new Requestmap(url: '/login/**', configAttribute: 	'IS_AUTHENTICATED_ANONYMOUSLY').save()
		new Requestmap(url: '/logout/**', configAttribute: 	'IS_AUTHENTICATED_ANONYMOUSLY').save()
		
		new Requestmap(url: '/leilao/create', configAttribute: 'ROLE_ADMIN').save()
		new Requestmap(url: '/leilao/list', configAttribute: 	'IS_AUTHENTICATED_ANONYMOUSLY').save()
		new Requestmap(url: '/leilao/show/**', configAttribute: 	'IS_AUTHENTICATED_ANONYMOUSLY').save()
		new Requestmap(url: '/leilao/delete**', configAttribute: 'ROLE_ADMIN').save()
		new Requestmap(url: '/leilao/edit**', configAttribute: 'ROLE_ADMIN').save()
		
		
		new Requestmap(url: '/produto/create', configAttribute: 'ROLE_ADMIN').save()
		new Requestmap(url: '/produto/list', configAttribute: 'IS_AUTHENTICATED_ANONYMOUSLY').save()
		new Requestmap(url: '/produto/show/**', configAttribute: 'IS_AUTHENTICATED_ANONYMOUSLY').save()
		new Requestmap(url: '/produto/delete**', configAttribute: 'ROLE_ADMIN').save()
		new Requestmap(url: '/produto/edit**', configAttribute: 'ROLE_ADMIN').save()
		

		// Inicializacao de categorias
		new Categoria(descricao:'Animais').save()
		new Categoria(descricao:'Brinquedos').save()
		new Categoria(descricao:'Games').save()
		new Categoria(descricao:'Joias e Relogios').save()
		new Categoria(descricao:'Saude e Beleza').save()
		new Categoria(descricao:'Eletronicos').save()
		new Categoria(descricao:'Veiculos').save()
		
		
	}

	def destroy = {
	}
}
